package cn.mygweb.admin.modules.web.service.impl;

import java.util.List;

import org.assertj.core.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.thymeleaf.util.StringUtils;

import cn.mygweb.admin.modules.web.mapper.ContentMapper;
import cn.mygweb.admin.modules.web.pojo.Content;
import cn.mygweb.admin.modules.web.service.ContentService;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/** 
* <p>Title: ContentServiceImpl.java</p>
* <p>Description: </p>  
* @author gxb 
* @date 2019年7月2日 
* <p>Copyright: Copyright (c) 2019</p>
* <p>Company: www.mygweb.cn</p> 
*/
@Service
public class ContentServiceImpl implements ContentService {

	@Autowired
	private ContentMapper contentMapper;
	
	/* (non-Javadoc)
	 * <p>Title: queryListByExample</p>
	 * <p>Description: </p>
	 * @return
	 * @see cn.mygweb.admin.modules.web.service.ContentService#queryListByExample()
	 */
	@Override
	@Transactional(propagation=Propagation.SUPPORTS)
	public List<Content> queryListByExample(Content content) {
		Example example = new Example(Content.class);
		Criteria criteria = example.createCriteria();
		if(null != content.getColumnId() || !StringUtils.isEmptyOrWhitespace(content.getColumnId())) {
			criteria.andCondition("column_id=", content.getColumnId());
		}
		List<Content> contentList = contentMapper.selectByExample(example);
		return contentList;
	}

	/* (non-Javadoc)
	 * <p>Title: queryById</p>
	 * <p>Description: </p>
	 * @param id
	 * @return
	 * @see cn.mygweb.admin.modules.web.service.ContentService#queryById(java.lang.String)
	 */
	@Override
	public Content queryById(String id) {
		return contentMapper.selectByPrimaryKey(id);
	}
	
	/* (non-Javadoc)
	 * <p>Title: save</p>
	 * <p>Description: </p>
	 * @param content
	 * @see cn.mygweb.admin.modules.web.service.ContentService#save(cn.mygweb.admin.modules.web.pojo.Content)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void save(Content content) {
		contentMapper.insert(content);
	}

	/* (non-Javadoc)
	 * <p>Title: batchDelete</p>
	 * <p>Description: </p>
	 * @param ids
	 * @see cn.mygweb.admin.modules.web.service.ContentService#batchDelete(java.util.List)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void batchDelete(String ids) {
		String[] idArr = ids.split(",");
		List<Object> idList = Arrays.asList(idArr);
		Example example = new Example(Content.class);
		Criteria criteria = example.createCriteria();
		criteria.andIn("id", idList);
		contentMapper.deleteByExample(example);
	}

	/* (non-Javadoc)
	 * <p>Title: update</p>
	 * <p>Description: </p>
	 * @param content
	 * @see cn.mygweb.admin.modules.web.service.ContentService#update(cn.mygweb.admin.modules.web.pojo.Content)
	 */
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void update(Content content) {
		contentMapper.updateByPrimaryKeySelective(content);
	}

}
